<?php

namespace App\Admin\Expand;

use App\Admin\Components\Room;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;

class Building extends LazyRenderable
{
    public function render()
    {
        $house_id = $this->key;
        $building = \App\Models\Building::query()
            ->where('house_id', $house_id)
            ->get(['id', 'name', 'floor', 'is_rent'])
            ->map(function ($value) {
                $value->is_all_string = $value->is_rent ? '是' : '否';
                $value->room_count = \App\Models\Room::query()->where('building_id', $value->id)->count();
                $string = "<a type='button' class='btn btn-primary' href='".route('admin.room.create')."?building_id=" . $value->id . "'>添加房号</a>&nbsp;";
                if ($value->room_count == 0) {
                    $value->setting  = $string
                    . "<a type='button' class='btn btn-primary' href='".route('admin.owner.create')."?building_id=" . $value->id . "'>整栋出租</a>";
                } else {
                    $value->setting = $string
                        . "<a type='button' class='btn btn-primary' href='".route('admin.room.index')."?building_id=" . $value->id . "'>房间列表</a>";
                }
                unset($value->is_rent);
                return $value;
            })->toArray();

        $title = [
            'Id',
            '楼栋名称',
            '层数',
            '是否整栋出租',
            '房间数量',
            '设置'
        ];
        return Table::make($title, $building);
    }
}
